package uicomponents;

import java.io.IOException;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

public class UIHistoricalMap extends UIComponentBase {
    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        context.getAttributes().remove("mapId");
        context.getAttributes().remove("sliderId");
    }

    @Override
    public void encodeEnd(FacesContext context) throws IOException {
        String id = (String)getAttributes().get("id");
        String mapId = (String)context.getAttributes().get("mapId");
        String yearSliderId = (String)context.getAttributes().get("yearSliderId");
        String daySliderId = (String)context.getAttributes().get("daySliderId");
        String eventProviderUrl = (String)getAttributes().get("eventProviderUrl");

        ResponseWriter writer = context.getResponseWriter();
        writer.startElement("script", this);
        writer.writeAttribute("type", "text/javascript", null);
        String script = id+" = new HistoricalMap("+mapId+", "+yearSliderId+", "+daySliderId+", '"+eventProviderUrl+"')";
        writer.writeText(script, null);
        writer.endElement("script");
    }

    @Override
    public String getFamily() {
        return "Rescriptus";
    }
}
